package com.ddwl.user.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.user.dao.mapper.RoleGrantMapper;
import com.ddwl.user.dao.model.RoleGrant;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author zach
 * @Date :2020-06-23 14:41.
 */
@Repository
public class RoleGrantDao extends TkDao<RoleGrantMapper,RoleGrant> {

    @Resource
    private RoleGrantMapper roleGrantMapper;

    /**
     * 清空用户菜单
     * @param roleId
     */
    public void deleteRoleMenu(String roleId) {
        RoleGrant roleMenu = new RoleGrant();
        roleMenu.setRoleId(roleId);
        roleGrantMapper.delete(roleMenu);
    }

    public List<RoleGrant> getRoleMenu(String roleId) {
        RoleGrant roleMenu = new RoleGrant();
        roleMenu.setRoleId(roleId);
        roleMenu.setType(0);
        return roleGrantMapper.select(roleMenu);
    }

    public void addRoleMenu(List<String> menuIds, String roleId) {
        List<RoleGrant>  roleGrants = new ArrayList<>();
        for (String menuId : menuIds) {
            RoleGrant roleGrant = new RoleGrant();
            roleGrant.setId(UUIDGenerator.getShortUUID());
            roleGrant.setGmtModified(Instant.now());
            roleGrant.setGmtCreated(Instant.now());
            roleGrant.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
            roleGrant.setRoleId(roleId);
            roleGrant.setGrantId(menuId);
            roleGrant.setType(0);
            roleGrants.add(roleGrant);
        }
        roleGrantMapper.batchInsertList(roleGrants);
    }

    public void changeId(Map<String, String> map) {
        roleGrantMapper.changeId(map);
    }

    public void changeRoleId(Map<String, String> map) {
        roleGrantMapper.changeRoleId(map);
    }

    public List<RoleGrant> getAll() {
        return roleGrantMapper.selectAll();
    }

    public void changeGrantId(Map<String, String> map) {
        roleGrantMapper.changeGrantId(map);
    }
}
